// backendPkg/pages/appointTimeManage/appointTimeManage.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
 
  data: {
    date: '',
    show: false,                 
    recordtime:[[{start:'08:00',end:'11:30'},{start:'11:30',end:'17:00'},{start:'17:05',end:'21:00'}],
    [{start:'08:00',end:'11:30'},{start:'11:30',end:'17:00'},{start:'17:05',end:'21:00'}],
    [{start:'08:00',end:'11:30'},{start:'11:30',end:'17:00'},{start:'17:05',end:'21:00'}]
    ],//预约时间段
    nowday:'',
    currentDateIndex: 0, // 初始时没有选中日期
    startDate: null, // 日历的开始日期

  },

  onDisplay() {
    this.setData({ show: true });
  
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  },

  onConfirm(e) {
    
    const clickedDate = new Date(e.detail); // e.detail 是你选择的日期
    clickedDate.setHours(0, 0, 0, 0);
    this.data.startDate.setHours(0, 0, 0, 0);
    const diffTime = Math.abs(clickedDate - this.data.startDate);
    const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 
    this.setData({
      currentDateIndex: diffDays,  //计算索引值
      show: false,
      date: this.formatDate(e.detail),
    });
    
  },
  handleStartInput: function(e) {
    const index = e.target.dataset.index;
    this.setData({
      [`recordtime[${this.data.currentDateIndex}][${index}].start`]: e.detail.value
    });
  },
  handleEndInput: function(e) {
    const index = e.target.dataset.index;
   
    this.setData({
      [`recordtime[${this.data.currentDateIndex}][${index}].end`]: e.detail.value
    });
  },
  
  handleAdd: function(e) {
    const newItem = { start: '', end: '' };
    const key = `recordtime[${this.data.currentDateIndex}]`;
   // console.log(this.data.currentDateIndex);
   // console.log(e)
    this.setData({
      [key]: [...this.data.recordtime[this.data.currentDateIndex], newItem],
    });
  
  },
  handleDelete: function(e) {
    const index = e.target.dataset.index;
    const key = `recordtime[${this.data.currentDateIndex}]`;
    const newArray = this.data.recordtime[this.data.currentDateIndex].filter((item, i) => i !== index);
    this.setData({
      [key]: newArray
    });
  },
  handleSave: function() {
    wx.setStorageSync('recordtime', this.data.recordtime);
    wx.showToast({
      title: '保存成功',
      icon: 'success'
    });
  },
  gettime(){
    let today = new Date(); // 创建一个新的Date对象，它将会包含当前的日期和时间
    let dd = String(today.getDate()).padStart(2, '0'); // 获取日，并转换为字符串，如果是单个数字则在前面补0
    let mm = String(today.getMonth() + 1).padStart(2, '0'); // 获取月份（注意：月份是从0开始的，所以需要+1），并转换为字符串，如果是单个数字则在前面补0
    let yyyy = today.getFullYear(); // 获取年份
    let currentDate = yyyy + '/' + mm + '/' + dd; // 拼接字符串
    this.setData({
      date:currentDate
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.gettime();
    this.setData({
      startDate: new Date(), // 日历的开始日期
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})